package com.fs.erp.api.basedata.controller.selector;

import com.fs.erp.api.basedata.bo.product.category.ProductCategoryTreeBo;
import com.fs.erp.api.basedata.entity.ProductCategory;
import com.fs.erp.api.basedata.service.ProductCategoryService;
import com.fs.erp.common.utils.CollectionUtil;
import com.fs.erp.web.controller.DefaultBaseController;
import com.fs.erp.web.resp.InvokeResult;
import com.fs.erp.web.resp.InvokeResultBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/selector")
public class SelectCategoryController extends DefaultBaseController {
    @Autowired
    private ProductCategoryService productCategoryService;

    @GetMapping("/category")
    public InvokeResult<List<ProductCategoryTreeBo>> queryCategory(){
        List<ProductCategory> categories=productCategoryService.getAllProductCategory();
        if (CollectionUtil.isEmpty(categories)){
            return InvokeResultBuilder.success(CollectionUtil.emptyList());
        }
        List<ProductCategoryTreeBo> bos=categories.stream().map(ProductCategoryTreeBo::new).collect(Collectors.toList());
        return InvokeResultBuilder.success(bos);
    }
}
